// miniprogram/pages/hb-luck/hb-luck.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fixed: !1,
  },


  getPhoneNumber(e) {
    var that = this;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      that.setData({
        mobile: res.result,
      })
      wx.setStorageSync('phone', res.result)
      console.log(res.result)
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 33,
          mobile: res.result,
          areaCode: that.data.area,
          agent: that.data.geneal,
          pageId: that.data.page
        },
        success: res => {
          console.log(res);
        }
      })
      var score = that.data.score;
      var ism = that.data.ism;
      var tel = that.data.tel;
      if (!ism) {
        wx.showToast({
          title: '请选择是否进面',
          icon: 'none'
        })
        return false;
      }
      if (score < 0 || score > 100 || score == undefined) {
        wx.showToast({
          title: '请填写正确的分数',
          icon: 'none'
        })
        return false;
      }
      if (tel != undefined) {
        console.log(tel)
        if (!/^1[3456789]\d{9}$/.test(tel)) {
          wx.showToast({
            title: '手机号格式错误',
            icon: 'none',
          });
          return;
        }
      }

      if (ism == '是') {
        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/choujiang/register?actid=39413&callback=?', //接收url
          data: {
            phone: res.result,
            ism: that.data.ism,
            fs: that.data.score,
            tel: that.data.tel,
            fenxiao: that.data.area,
            geneal: that.data.geneal
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson)
            if (theJson.status == 1) {
              wx.request({ //发送请求
                url: 'https://zg99.offcn.com/index/choujiang/lottery?actid=39413&callback=?', //接收url
                data: {
                  phone: that.data.mobile,
                }, //接收参数
                success(res) {
                  var jsonStr = res.data;
                  jsonStr = jsonStr.replace("(", "");
                  jsonStr = jsonStr.replace(")", "");
                  if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                  }
                  console.log(theJson)
                  var prizeid = theJson.prizeid
               
                  if (prizeid == 1) {
                    var res = Math.random()
                    var jjres = res.toFixed(2)
                    wx.setStorageSync('jjres', jjres)
                  } else if (prizeid == 2) {
                    var res = Math.random()*(2-1+1)+1
                    var jjres = res.toFixed(2)
                    wx.setStorageSync('jjres', jjres)
                  } else if (prizeid == 3) {
                    var res = Math.random()*(20-3+1)+3
                    var jjres = res.toFixed(2)
                    wx.setStorageSync('jjres', jjres)
                  } else if (prizeid == 4) {
                    var res = Math.random()*(50-20+1)+20
                    var jjres = res.toFixed(2)
                    wx.setStorageSync('jjres', jjres)
                  }
                  wx.redirectTo({
                    url: '../hb-res/hb-res',
                  })
                }
              })
            } else {
              wx.redirectTo({
                url: '../hb-res/hb-res',
              })
            }

          }
        })
      } else {
        wx.showToast({
          title: '未进面无法参加活动',
          icon: 'none'
        })
      }




    }).catch(err => {
      console.error(err);
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
  },
  scoreInput: function (e) {
    this.setData({
      score: e.detail.value
    })
    console.log(this.data.score)
  },
  telInput: function (e) {
    this.setData({
      tel: e.detail.value
    })
    console.log(this.data.tel)
  },
  close: function (e) {
    var that = this;
    that.setData({
      fixed: !1
    })
  },
  tips: function (e) {
    var that = this;
    that.setData({
      fixed: 1
    })
  },
  yes: function (e) {
    var that = this;
    that.setData({
      ism: e.target.dataset.in,
    })
    console.log(that.data.ism)
  },
  no: function (e) {
    var that = this;
    that.setData({
      ism: e.target.dataset.in
    })
    console.log(that.data.ism)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**右上角分享*/
  onShareAppMessage: function () {
    var that = this;
    console.log(that.data.p_open_id)
    return {
      title: '山东中公进面红包领取’',
      path: 'pages/hb-index/hb-index?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page
    }
  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
    var that = this;
    console.log(that.data.p_open_id)
    return {
      title: '山东中公进面红包领取’',
      path: 'pages/hb-index/hb-index?area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page
    }

  }
})